package com.example.a30;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

    public static final String COLUMN_ID = "id";
    public static final String COLUMN_AMOUNT = "amount";
    public static final String COLUMN_DATE = "date";
    public static final String COLUMN_CATEGORY = "category";

    private static final String DATABASE_NAME = "expenses.db";
    private static final String TABLE_EXPENSES = "expenses";

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE " + TABLE_EXPENSES +
                "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                COLUMN_AMOUNT + " REAL," +
                COLUMN_DATE + " TEXT," +
                COLUMN_CATEGORY + " TEXT" +
                ")";
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_EXPENSES);
        onCreate(db);
    }

    public long addExpense(double amount, String date, String category) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_AMOUNT, amount);
        values.put(COLUMN_DATE, date);
        values.put(COLUMN_CATEGORY, category);
        return db.insert(TABLE_EXPENSES, null, values);
    }

    public Cursor getAllExpenses() {
        SQLiteDatabase db = getReadableDatabase();
        String[] columns = {COLUMN_ID, COLUMN_AMOUNT, COLUMN_DATE, COLUMN_CATEGORY};
        return db.query(TABLE_EXPENSES, columns, null, null, null, null, null);
    }

    public double getTotalExpenseByCategory(String category) {
        SQLiteDatabase db = getReadableDatabase();
        String selection = COLUMN_CATEGORY + " = ?";
        String[] selectionArgs = {category};
        String[] columns = {"SUM(" + COLUMN_AMOUNT + ")"};
        Cursor cursor = db.query(TABLE_EXPENSES, columns, selection, selectionArgs, null, null, null);
        double totalExpense = 0.0;
        if (cursor.moveToFirst()) {
            totalExpense = cursor.getDouble(0);
        }
        cursor.close();
        return totalExpense;
    }

    public double getTotalDailyExpense() {
        return getTotalExpenseByCategory("Daily Expense");
    }

    public double getTotalEntertainmentExpense() {
        return getTotalExpenseByCategory("Entertainment");
    }

    public double getTotalOtherExpense() {
        return getTotalExpenseByCategory("Other");
    }

    public long addDailyExpense(String dateString) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_AMOUNT, 0.0);
        values.put(COLUMN_DATE, dateString);
        values.put(COLUMN_CATEGORY, "Daily Expense");
        return db.insert(TABLE_EXPENSES, null, values);
    }

    public long addEntertainmentExpense(String dateString) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_AMOUNT, 0.0);
        values.put(COLUMN_DATE, dateString);
        values.put(COLUMN_CATEGORY, "Entertainment");
        return db.insert(TABLE_EXPENSES, null, values);
    }

    public long addOtherExpense(String dateString) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_AMOUNT, 0.0);
        values.put(COLUMN_DATE, dateString);
        values.put(COLUMN_CATEGORY, "Other");
        return db.insert(TABLE_EXPENSES, null, values);
    }

    public void resetDailyExpense() {
        SQLiteDatabase db = getWritableDatabase();
        String whereClause = COLUMN_CATEGORY + " = ?";
        String[] whereArgs = {"Daily Expense"};
        ContentValues values = new ContentValues();
        values.put(COLUMN_AMOUNT, 0.0);
        db.update(TABLE_EXPENSES, values, whereClause, whereArgs);
    }
}
